package br.com.vdn.bean;

import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import br.com.util.exception.UtilException;
import br.com.util.jsf.AbstractBean;
import br.com.util.jsf.JSFUtil;
import br.com.util.tipodado.DataHora;
import br.com.util.tipodado.Flag;
import br.com.util.tipodado.YesNo;
import br.com.vdn.negocio.VendedorNgc;
import br.com.vdn.vo.UsuarioVo;
import br.com.vdn.vo.VendedorVo;

public class VendedorBean extends AbstractBean<VendedorVo, VendedorNgc>
{
	private JSFUtil util = new JSFUtil();
	
	public VendedorBean()
	{
		super(VendedorNgc.getInstancia());
		this.ACTION_SEARCH = "vendedor";
		this.pageTitle = "Vendedor";
	}
	
	@Override
	protected void completarPesquisar() throws UtilException
	{
		this.getSearchObject().setFlagAtivo(new Flag(YesNo.YES));
	}
	
	@Override
	public void inserir(ActionEvent event)
	{
		try
		{
			validarCamposObrigatorios();
			
			completarInserir();
			setEntity(VendedorNgc.getInstancia().inserir(getEntity()));
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(FacesMessage.SEVERITY_INFO, util.getBundle().getString("item.incluido.sucesso"), ""));

			this.cancelar(event);
		}
		catch (UtilException e)
		{
			FacesMessage message = new FacesMessage(e.getMensagem());
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			FacesContext.getCurrentInstance().addMessage(null, message);
		}
	}
	
	@Override
	public void alterar(ActionEvent event)
	{
		try
		{
			validarCamposObrigatorios();
			
			completarAlterar();
			setEntity(VendedorNgc.getInstancia().alterar(getEntity()));
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(FacesMessage.SEVERITY_INFO, util.getBundle().getString("item.alterado.sucesso"), ""));
			
			this.cancelar(event);
			
		} 
		catch (UtilException e)
		{
			FacesMessage message = new FacesMessage(e.getMensagem());
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			FacesContext.getCurrentInstance().addMessage(null, message);
		}
	}
	
	private void validarCamposObrigatorios() throws UtilException
	{
		if (this.getEntity().getNome() == null
				|| this.getEntity().getNome().getValor() == null
				|| "".equalsIgnoreCase(this.getEntity().getNome().getValor()))
		{
			throw new UtilException("campo.obrigatorio", new String[]{"Nome"});
		}
		
		if (this.getEntity().getCpf() != null
				&& this.getEntity().getCpf().isPreenchido()
				&& !this.getEntity().getCpf().isValido())
		{
			throw new UtilException("campo.invalido", new String[]{"CPF"});
		}
	}
	
	@Override
	protected void completarInserir() throws UtilException
	{
		this.getEntity().setFlagAtivo(new Flag(YesNo.YES));
		this.getEntity().setDataInclusao(new DataHora(new Date().getTime()));
		this.getEntity().setUsuarioInclusaoVo((UsuarioVo) util.getSession().getAttribute("usuarioLogado"));
	}
	
	public void inativar(ActionEvent event)
	{
		try
		{
			VendedorVo inativar = this.getSelectedRowEntity();
			inativar.setFlagAtivo(new Flag(YesNo.NO));
			inativar.setDataAlteracao(new DataHora(new Date().getTime()));
			inativar.setUsuarioAlteracaoVo((UsuarioVo) util.getSession().getAttribute("usuarioLogado"));
			VendedorNgc.getInstancia().alterar(inativar);
			
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, 
					util.getBundle().getString("item.inativado.sucesso"), null));
			
			this.cancelar(event);
		}
		catch (UtilException ue)
		{
			FacesMessage message = new FacesMessage(ue.getMensagem());  
	        message.setSeverity(FacesMessage.SEVERITY_ERROR);  
	        FacesContext.getCurrentInstance().addMessage(null, message);
		}
	}
	
}